defmodule Explorer.ChainSpec.Geth.ImporterTest do
  use Explorer.DataCase

  import Mox
  import EthereumJSONRPC, only: [integer_to_quantity: 1]

  alias Explorer.Chain.Address.{CoinBalance, CoinBalanceDaily}
  alias Explorer.Chain.{Address, Hash}
  alias Explorer.ChainSpec.Geth.Importer
  alias Explorer.Repo

  setup :set_mox_global

  setup :verify_on_exit!

  @geth_genesis "#{File.cwd!()}/test/support/fixture/chain_spec/qdai_genesis.json"
                |> File.read!()
                |> Jason.decode!()

  @polygon_genesis "#{File.cwd!()}/test/support/fixture/chain_spec/polygon_genesis.json"
                   |> File.read!()
                   |> Jason.decode!()

  @optimism_genesis "#{File.cwd!()}/test/support/fixture/chain_spec/optimism_genesis.json"
                    |> File.read!()
                    |> Jason.decode!()

  @zkatana_genesis "#{File.cwd!()}/test/support/fixture/chain_spec/zkatana_genesis.json"
                   |> File.read!()
                   |> Jason.decode!()

  describe "genesis_accounts/1" do
    test "parses coin balance and contract code" do
      coin_balances = Importer.genesis_accounts(@geth_genesis)

      assert Enum.count(coin_balances) == 3

      assert %{
               address_hash: %Hash{
                 byte_count: 20,
                 bytes: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 254>>
               },
               value: 0,
               contract_code:
                 "0x6080604052600436106100cf5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630f3a053381146100d457806310f2ee7c146101075780632ee57f8d1461011c57806330f6eb16146101665780633d84b8c11461018a5780634476d66a146101ab578063553a5c85146101c3578063899ca7fc146101d8578063aa9fa27414610225578063b4a523e81461024b578063db456f771461026c578063e73b9e2f146102a0578063efdc4d01146102c1578063f91c2898146102d6575b600080fd5b3480156100e057600080fd5b506100f5600160a060020a036004351661039b565b60408051918252519081900360200190f35b34801561011357600080fd5b506100f5610464565b34801561012857600080fd5b50610131610469565b604080517fffffffff000000000000000000000000000000000000000000000000000000009092168252519081900360200190f35b34801561017257600080fd5b506100f5600160a060020a036004351660243561049e565b34801561019657600080fd5b506100f5600160a060020a0360043516610570565b3480156101b757600080fd5b506100f56004356105f7565b3480156101cf57600080fd5b506100f561066f565b3480156101e457600080fd5b506101ed6106bd565b6040518082602080838360005b838110156102125781810151838201526020016101fa565b5050505090500191505060405180910390f35b34801561023157600080fd5b50610249600435600160a060020a03602435166106eb565b005b34801561025757600080fd5b506100f5600160a060020a03600435166107bd565b34801561027857600080fd5b50610284600435610844565b60408051600160a060020a039092168252519081900360200190f35b3480156102ac57600080fd5b506100f5600160a060020a0360043516610908565b3480156102cd57600080fd5b506100f561098f565b3480156102e257600080fd5b5061030260246004803582810192908201359181359182019101356109dd565b604051808060200180602001838103835285818151815260200191508051906020019060200280838360005b8381101561034657818101518382015260200161032e565b50505050905001838103825284818151815260200191508051906020019060200280838360005b8381101561038557818101518382015260200161036d565b5050505090500194505050505060405180910390f35b604080517f0f09dbb26898a3af738d25c5fff308337ac8f2b0acbbaf209b373fb1389bcf2f602080830191909152606060020a600160a060020a038516028284015282516034818403018152605490920192839052815160009384938493909282918401908083835b602083106104235780518252601f199092019160209182019101610404565b51815160209384036101000a600019018019909216911617905260408051929094018290039091208652850195909552929092016000205495945050505050565b600181565b604080517f626c6f636b5265776172640000000000000000000000000000000000000000008152905190819003600b01902090565b604080517f24ae442c1f305c4f1294bf2dddd491a64250b2818b446706e9a74aeaaaf6f419602080830191909152606060020a600160a060020a0386160282840152605480830185905283518084039091018152607490920192839052815160009384938493909282918401908083835b6020831061052e5780518252601f19909201916020918201910161050f565b51815160209384036101000a60001901801990921691161790526040805192909401829003909120865285019590955292909201600020549695505050505050565b604080517f0fd3be07b1332be84678873bf53feb10604cd09244fb4bb9154e03e00709b9e7602080830191909152606060020a600160a060020a03851602828401528251603481840301815260549092019283905281516000938493849390928291840190808383602083106104235780518252601f199092019160209182019101610404565b604080517f3840e646f7ce9b3210f5440e2dbd6b36451169bfdac65ef00a161729eded81bd60208083019190915281830184905282518083038401815260609092019283905281516000938493849390928291840190808383602083106104235780518252601f199092019160209182019101610404565b7f076e79ca1c3a46f0c7d1e9e7f14bcb9716bfc49eed37baf510328301a7109c2560009081526020527fec9588bd3242595c0e33049f6384a658ee56f04f9b9e85c6cc9a045c4948c9515490565b6106c56112f3565b50604080516020810190915273bf3d6f830ce263cae987193982192cd990442b53815290565b60006106f633610baf565b151561070157600080fd5b82151561070d57600080fd5b600160a060020a038216151561072257600080fd5b61072b8261039b565b905080151561073d5761073d82610c1a565b610756610750828563ffffffff610d5716565b83610d6a565b6107786107728461076633610908565b9063ffffffff610d5716565b33610e34565b6040805184815290513391600160a060020a038516917f3c798bbcf33115b42c728b8504cff11dd58736e9fa789f1cda2738db7d696b2a9181900360200190a3505050565b604080517f12e71282a577e2b463da2c18bc96b6122db29bcef9065ed5a7f0f9316c11c08e602080830191909152606060020a600160a060020a03851602828401528251603481840301815260549092019283905281516000938493849390928291840190808383602083106104235780518252601f199092019160209182019101610404565b604080517fa47da669ec9f3749fbb12db00588b5fa6b5bbd24da81eb6cab44261334c21c1760208083019190915281830184905282518083038401815260609092019283905281516000936002938593909282918401908083835b602083106108be5780518252601f19909201916020918201910161089f565b51815160209384036101000a6000190180199092169116179052604080519290940182900390912086528501959095529290920160002054600160a060020a031695945050505050565b604080517fa7f48dc57b1a051b1732e5ed136bbfd33bb5aa418e3e3498901320529e785461602080830191909152606060020a600160a060020a03851602828401528251603481840301815260549092019283905281516000938493849390928291840190808383602083106104235780518252601f199092019160209182019101610404565b7f0678259008a66390de8a5ac3f500d1dfb0d0f57018441e2cc69aaa0f52c97d4460009081526020527f3f9dbe5402519a8ea505664ae3f65100b338acc0e57c0abec1fcff383511ac4f5490565b60608060008180828080808033156109f457600080fd5b8c156109ff57600080fd5b8a15610a0a57600080fd5b610a1261098f565b975087604051908082528060200260200182016040528015610a3e578160200160208202803883390190505b50965087604051908082528060200260200182016040528015610a6b578160200160208202803883390190505b509550600094505b87851015610ae757610a8485610844565b9350610a8f8461039b565b9250610a9c600085610d6a565b838786815181101515610aab57fe5b600160a060020a0390921660209283029091019091015285518390879087908110610ad257fe5b60209081029091010152600190940193610a73565b600094505b87851015610b3757610b2c8686815181101515610b0557fe5b906020019060200201518887815181101515610b1d57fe5b90602001906020020151610ebb565b600190940193610aec565b600094505b6001851015610b9357610b4d6106bd565b8560018110610b5857fe5b60200201519150610b6882610908565b90506000811115610b8857610b7e600083610e34565b610b8881836111c4565b600190940193610b3c565b610b9b6112a4565b50949c939b50929950505050505050505050565b6000610bb96112f3565b6000610bc36106bd565b9150600090505b6001811015610c0e57818160018110610bdf57fe5b6020020151600160a060020a031684600160a060020a03161415610c065760019250610c13565b600101610bca565b600092505b5050919050565b6000610c2461098f565b604080517fa47da669ec9f3749fbb12db00588b5fa6b5bbd24da81eb6cab44261334c21c176020808301919091528183018490528251808303840181526060909201928390528151939450859360029360009392909182918401908083835b60208310610ca25780518252601f199092019160209182019101610c83565b51815160209384036101000a6000190180199092169116179052604080519290940182900390912086528581019690965250929092016000908120805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a03969096169590951790945550507f0678259008a66390de8a5ac3f500d1dfb0d0f57018441e2cc69aaa0f52c97d448252526001017f3f9dbe5402519a8ea505664ae3f65100b338acc0e57c0abec1fcff383511ac4f5550565b81810182811015610d6457fe5b92915050565b604080517f0f09dbb26898a3af738d25c5fff308337ac8f2b0acbbaf209b373fb1389bcf2f602080830191909152606060020a600160a060020a038516028284015282516034818403018152605490920192839052815185936000938493909282918401908083835b60208310610df25780518252601f199092019160209182019101610dd3565b51815160209384036101000a60001901801990921691161790526040805192909401829003909120865285019590955292909201600020939093555050505050565b604080517fa7f48dc57b1a051b1732e5ed136bbfd33bb5aa418e3e3498901320529e785461602080830191909152606060020a600160a060020a0385160282840152825160348184030181526054909201928390528151859360009384939092829184019080838360208310610df25780518252601f199092019160209182019101610dd3565b604080517f24ae442c1f305c4f1294bf2dddd491a64250b2818b446706e9a74aeaaaf6f419602080830191909152606060020a600160a060020a038516028284015243605480840191909152835180840390910181526074909201928390528151600093918291908401908083835b60208310610f495780518252601f199092019160209182019101610f2a565b51815160209384036101000a60001901801990921691161790526040805192909401829003822060008181528083528590208a90557f0fd3be07b1332be84678873bf53feb10604cd09244fb4bb9154e03e00709b9e783830152600160a060020a038916606060020a02838601528451808403603401815260549093019485905282519097509195509293508392850191508083835b60208310610ffe5780518252601f199092019160209182019101610fdf565b51815160209384036101000a60001901801990921691161790526040805192909401829003909120600081815291829052929020549194506110469350909150859050610d57565b600082815260208181526040918290209290925580517f3840e646f7ce9b3210f5440e2dbd6b36451169bfdac65ef00a161729eded81bd818401524381830152815180820383018152606090910191829052805190928291908401908083835b602083106110c55780518252601f1990920191602091820191016110a6565b51815160209384036101000a600019018019909216911617905260408051929094018290039091206000818152918290529290205491945061110d9350909150859050610d57565b6000828152602081905260408120919091557f076e79ca1c3a46f0c7d1e9e7f14bcb9716bfc49eed37baf510328301a7109c2590527fec9588bd3242595c0e33049f6384a658ee56f04f9b9e85c6cc9a045c4948c95154611174908463ffffffff610d5716565b7f076e79ca1c3a46f0c7d1e9e7f14bcb9716bfc49eed37baf510328301a7109c2560009081526020527fec9588bd3242595c0e33049f6384a658ee56f04f9b9e85c6cc9a045c4948c95155505050565b604080517f12e71282a577e2b463da2c18bc96b6122db29bcef9065ed5a7f0f9316c11c08e602080830191909152606060020a600160a060020a0385160282840152825160348184030181526054909201928390528151600093918291908401908083835b602083106112485780518252601f199092019160209182019101611229565b51815160209384036101000a60001901801990921691161790526040805192909401829003909120600081815291829052929020549194506112909350909150859050610d57565b600091825260208290526040909120555050565b7f0678259008a66390de8a5ac3f500d1dfb0d0f57018441e2cc69aaa0f52c97d44600090815260208190527f3f9dbe5402519a8ea505664ae3f65100b338acc0e57c0abec1fcff383511ac4f55565b60206040519081016040528060019060208202803883395091929150505600a165627a7a7230582053b0e89d867fc0c586739f4911c11be5aaee046320d1dff0da51c1b04404b4a00029"
             } ==
               List.first(coin_balances)
    end

    test "parses polygon coin balance and contract code" do
      coin_balances = Importer.genesis_accounts(@polygon_genesis)

      assert Enum.count(coin_balances) == 2

      assert %{
               address_hash: %Explorer.Chain.Hash{
                 byte_count: 20,
                 bytes: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1>>
               },
               value: 0,
               contract_code:
                 "0x608060405234801561001057600080fd5b50600436106101b05760003560e01c806370a08231116100ef578063ce513b6f11610092578063ce513b6f14610398578063dd62ed3e146103ab578063e0563ab1146103be578063ea0fee4f146103c7578063eacdc5ff146103cf578063eeb49945146103d8578063f3f43703146103eb578063fd242c14146103fe57600080fd5b806370a08231146102e7578063947287cf146102fa57806395d89b411461030357806397e5230d1461030b578063981b24d014610315578063a457c2d714610328578063a9059cbb1461033b578063c6b61e4c1461034e57600080fd5b8063395093511161015757806339509351146102735780633b878c22146102865780633ccfd60b1461028f5780633fd50001146102975780634ee2cd7e146102aa57806351351d53146102bd57806361cc2763146102cb57806362656003146102de57600080fd5b806306fdde03146101b5578063095ea7b3146101d35780630f50287c146101f657806318160ddd1461020b57806323b872dd1461021d578063284017f5146102305780632e17de7814610251578063313ce56714610264575b600080fd5b6101bd610411565b6040516101ca91906119ba565b60405180910390f35b6101e66101e13660046119e2565b6104a3565b60405190151581526020016101ca565b610209610204366004611a0e565b6104bd565b005b6035545b6040519081526020016101ca565b6101e661022b366004611a46565b61074f565b61023961202081565b6040516001600160a01b0390911681526020016101ca565b61020961025f366004611a87565b610773565b604051601281526020016101ca565b6101e66102813660046119e2565b61078a565b61023961101081565b6102096107ac565b61020f6102a5366004611a87565b6108bd565b61020f6102b83660046119e2565b6108de565b6102396002600160a01b0381565b6102096102d9366004611b10565b6108f1565b61020f60cc5481565b61020f6102f5366004611c29565b610b1f565b61020f61520881565b6101bd610b3a565b61020f620249f081565b61020f610323366004611a87565b610b49565b6101e66103363660046119e2565b610b54565b6101e66103493660046119e2565b610bcf565b61037d61035c366004611a87565b60ce6020526000908152604090208054600182015460029092015490919083565b604080519384526020840192909252908201526060016101ca565b61020f6103a6366004611c29565b610bdd565b61020f6103b9366004611c46565b610c0b565b61023961203081565b61020f600181565b61020f60cd5481565b6102096103e6366004611c7f565b610c36565b61020f6103f9366004611c29565b610d08565b61020f61040c366004611a87565b610d2f565b60606036805461042090611d08565b80601f016020809104026020016040519081016040528092919081815260200182805461044c90611d08565b80156104995780601f1061046e57610100808354040283529160200191610499565b820191906000526020600020905b81548152906001019060200180831161047c57829003601f168201915b5050505050905090565b6000336104b1818585610d79565b60019150505b92915050565b336002600160a01b03146105065760405163973d02cb60e01b815260206004820152600a60248201526914d654d5115350d0531360b21b60448201526064015b60405180910390fd5b60cd80546000918261051783611d58565b9190505590508083146105625760405162461bcd60e51b815260206004820152601360248201527215539156141150d5115117d15413d0d217d251606a1b60448201526064016104fd565b81356020830135116105ac5760405162461bcd60e51b81526020600482015260136024820152721393d7d09313d0d2d4d7d0d3d3535255151151606a1b60448201526064016104fd565b60cc546105be83356020850135611d71565b6105c9906001611d84565b6105d39190611dad565b1561062e5760405162461bcd60e51b815260206004820152602560248201527f45504f43485f4d5553545f42455f444956495349424c455f42595f45504f43486044820152645f53495a4560d81b60648201526084016104fd565b813560ce600061063f600185611d71565b815260200190815260200160002060010154600161065d9190611d84565b146106a05760405162461bcd60e51b8152602060048201526013602482015272494e56414c49445f53544152545f424c4f434b60681b60448201526064016104fd565b600081815260ce6020526040902082906106d182828135815560208201356001820155604082013560028201555050565b505060cf80546001810182556000919091526020838101357facb8d954e2cfef495862221e91bd7523613cf8808827cb33edfe4904cc51bf299092018290556040805190850135815284359186917f0ce8712c4dee4bd5a691f0bc1c39594671591e77395f8ebf6a3fb5f63fbea66a910160405180910390a4505050565b60003361075d858285610e9e565b610768858585610f12565b506001949350505050565b61077d33826110b6565b61078733826111e1565b50565b6000336104b181858561079d8383610c0b565b6107a79190611d84565b610d79565b33600090815260d06020526040812060cd5490919081906107ce90849061125a565b808555604051828152919350915033907f7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b659060200160405180910390a260c95460cb54604080517f8ca9a95e41b5eece253c93f5b31eed1253aed6b145d8a6e14d913fdf8e7322936020820152338183015260608082018790528251808303909101815260808201928390526316f1983160e01b9092526001600160a01b03938416936316f198319361088693911691608401611dc1565b600060405180830381600087803b1580156108a057600080fd5b505af11580156108b4573d6000803e3d6000fd5b50505050505050565b60cf81815481106108cd57600080fd5b600091825260209091200154905081565b60006108ea83836112cc565b9392505050565b600054610100900460ff16158080156109115750600054600160ff909116105b8061092b5750303b15801561092b575060005460ff166001145b61098e5760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b60648201526084016104fd565b6000805460ff1916600117905580156109b1576000805461ff0019166101001790555b6109fb6040518060400160405280600c81526020016b15985b1a59185d1bdc94d95d60a21b815250604051806040016040528060048152602001631594d15560e21b815250611315565b60c980546001600160a01b038089166001600160a01b03199283161790925560ca805488841690831617905560cb80549287169290911691909117905560cc83905560005b8251811015610a9557610a8d838281518110610a5e57610a5e611de5565b602002602001015160000151848381518110610a7c57610a7c611de5565b60200260200101516020015161134a565b600101610a40565b5060cf80546001818101835560009283527facb8d954e2cfef495862221e91bd7523613cf8808827cb33edfe4904cc51bf299091019190915560cd558015610b17576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b505050505050565b6001600160a01b031660009081526033602052604090205490565b60606037805461042090611d08565b60006104b782611354565b60003381610b628286610c0b565b905083811015610bc25760405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f77604482015264207a65726f60d81b60648201526084016104fd565b6107688286868403610d79565b6000336104b1818585610f12565b60cd546001600160a01b038216600090815260d0602052604081209091610c04919061125a565b5092915050565b6001600160a01b03918216600090815260346020908152604080832093909416825291909152205490565b60ca546001600160a01b031633148015610c5d575060cb546001600160a01b038481169116145b610c9a5760405162461bcd60e51b815260206004820152600e60248201526d24a72b20a624a22fa9a2a72222a960911b60448201526064016104fd565b7f1bcc0f4c3fad314e585165815f94ecca9b96690a26d6417d7876448a9a867a69610cc9602060008486611dfb565b610cd291611e25565b03610d0257600080610ce78360208187611dfb565b810190610cf491906119e2565b91509150610b17828261134a565b50505050565b60cd546001600160a01b038216600090815260d06020526040812090916104b7919061137f565b600081815260ce60205260408120600101548015610d7057600083815260ce6020526040902054610d609082611d71565b610d6b906001611d84565b6108ea565b60009392505050565b6001600160a01b038316610ddb5760405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b60648201526084016104fd565b6001600160a01b038216610e3c5760405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b60648201526084016104fd565b6001600160a01b0383811660008181526034602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92591015b60405180910390a3505050565b6000610eaa8484610c0b565b90506000198114610d025781811015610f055760405162461bcd60e51b815260206004820152601d60248201527f45524332303a20696e73756666696369656e7420616c6c6f77616e636500000060448201526064016104fd565b610d028484848403610d79565b6001600160a01b038316610f765760405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f206164604482015264647265737360d81b60648201526084016104fd565b6001600160a01b038216610fd85760405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b60648201526084016104fd565b610fe383838361141d565b6001600160a01b0383166000908152603360205260409020548181101561105b5760405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e7420657863656564732062604482015265616c616e636560d01b60648201526084016104fd565b6001600160a01b038085166000818152603360205260408082208686039055928616808252908390208054860190559151600080516020611fd6833981519152906110a99086815260200190565b60405180910390a3610d02565b6001600160a01b0382166111165760405162461bcd60e51b815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f206164647265736044820152607360f81b60648201526084016104fd565b6111228260008361141d565b6001600160a01b038216600090815260336020526040902054818110156111965760405162461bcd60e51b815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e604482015261636560f01b60648201526084016104fd565b6001600160a01b0383166000818152603360209081526040808320868603905560358054879003905551858152919291600080516020611fd68339815191529101610e91565b505050565b61121381600160cd546111f49190611d84565b6001600160a01b038516600090815260d0602052604090209190611486565b816001600160a01b03167f655c1cd0236fb6dc4916f34c8ff10e3b18fcaea5b344dfc16c36fbb1bdfc5df28260405161124e91815260200190565b60405180910390a25050565b81546000905b83600101548110156112c5576000818152600285016020908152604091829020825180840190935280548352600101549082018190528410156112a357506112c5565b80516112af9084611d84565b92505080806112bd90611d58565b915050611260565b9250929050565b6001600160a01b0382166000908152606560205260408120819081906112f39085906115b1565b915091508161130a5761130585610b1f565b61130c565b805b95945050505050565b600054610100900460ff1661133c5760405162461bcd60e51b81526004016104fd90611e43565b611346828261169f565b5050565b61134682826116df565b60008060006113648460666115b1565b915091508161137557603554611377565b805b949350505050565b60018201546000908082036113985760009150506104b7565b60006113a5600183611d71565b90505b845481106114155760008181526002860160209081526040918290208251808401909352805483526001015490820181905285106113e65750611415565b80516113f29085611d84565b9350816000036114025750611415565b508061140d81611e8e565b9150506113a8565b505092915050565b6001600160a01b038316158061143a57506001600160a01b038216155b61147b5760405162461bcd60e51b81526020600482015260126024820152712a2920a729a322a92fa327a92124a22222a760711b60448201526064016104fd565b6111dc83838361179a565b8160000361149657611496611ea5565b825460018401548181036114ed576040805180820182528581526020808201868152600085815260028a0190925292812091518255915160019182015586018054916114e183611d58565b91905055505050505050565b600060028601816114ff600185611d71565b81526020019081526020016000206001015490508084101561152357611523611ea5565b83811015611572576040805180820182528681526020808201878152600086815260028b01909252928120915182559151600191820155870180549161156883611d58565b9190505550610b17565b84600287016000611584600186611d71565b815260200190815260200160002060000160008282546115a49190611d84565b9091555050505050505050565b600080600084116115fd5760405162461bcd60e51b815260206004820152601660248201527504552433230536e617073686f743a20696420697320360541b60448201526064016104fd565b60cd5484111561164f5760405162461bcd60e51b815260206004820152601d60248201527f4552433230536e617073686f743a206e6f6e6578697374656e7420696400000060448201526064016104fd565b600061165b84866117e2565b845490915081036116735760008092509250506112c5565b600184600101828154811061168a5761168a611de5565b906000526020600020015492509250506112c5565b600054610100900460ff166116c65760405162461bcd60e51b81526004016104fd90611e43565b60366116d28382611f01565b5060376111dc8282611f01565b6001600160a01b0382166117355760405162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f20616464726573730060448201526064016104fd565b6117416000838361141d565b80603560008282546117539190611d84565b90915550506001600160a01b038216600081815260336020908152604080832080548601905551848152600080516020611fd6833981519152910160405180910390a35050565b6001600160a01b0383166117b9576117b18261188f565b6111dc6118b9565b6001600160a01b0382166117d0576117b18361188f565b6117d98361188f565b6111dc8261188f565b815460009081036117f5575060006104b7565b82546000905b8082101561184257600061180f83836118c9565b6000878152602090209091508590820154111561182e5780915061183c565b611839816001611d84565b92505b506117fb565b60008211801561186e57508361186b8661185d600186611d71565b600091825260209091200190565b54145b156118875761187e600183611d71565b925050506104b7565b5090506104b7565b6001600160a01b0381166000908152606560205260409020610787906118b483610b1f565b6118e4565b6118c760666118b460355490565b565b60006118d86002848418611fc1565b6108ea90848416611d84565b60006118ef60cd5490565b9050806118fb8461192f565b10156111dc578254600180820185556000858152602080822090930193909355938401805494850181558252902090910155565b8054600090810361194257506000919050565b8154829061195290600190611d71565b8154811061196257611962611de5565b90600052602060002001549050919050565b6000815180845260005b8181101561199a5760208185018101518683018201520161197e565b506000602082860101526020601f19601f83011685010191505092915050565b6020815260006108ea6020830184611974565b6001600160a01b038116811461078757600080fd5b600080604083850312156119f557600080fd5b8235611a00816119cd565b946020939093013593505050565b6000808284036080811215611a2257600080fd5b833592506060601f1982011215611a3857600080fd5b506020830190509250929050565b600080600060608486031215611a5b57600080fd5b8335611a66816119cd565b92506020840135611a76816119cd565b929592945050506040919091013590565b600060208284031215611a9957600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6040805190810167ffffffffffffffff81118282101715611ad957611ad9611aa0565b60405290565b604051601f8201601f1916810167ffffffffffffffff81118282101715611b0857611b08611aa0565b604052919050565b600080600080600060a08688031215611b2857600080fd5b8535611b33816119cd565b9450602086810135611b44816119cd565b9450604087810135611b55816119cd565b945060608801359350608088013567ffffffffffffffff80821115611b7957600080fd5b818a0191508a601f830112611b8d57600080fd5b813581811115611b9f57611b9f611aa0565b611bad858260051b01611adf565b818152858101925060069190911b83018501908c821115611bcd57600080fd5b928501925b81841015611c165784848e031215611bea5760008081fd5b611bf2611ab6565b8435611bfd816119cd565b8152848701358782015283529284019291850191611bd2565b8096505050505050509295509295909350565b600060208284031215611c3b57600080fd5b81356108ea816119cd565b60008060408385031215611c5957600080fd5b8235611c64816119cd565b91506020830135611c74816119cd565b809150509250929050565b60008060008060608587031215611c9557600080fd5b843593506020850135611ca7816119cd565b9250604085013567ffffffffffffffff80821115611cc457600080fd5b818701915087601f830112611cd857600080fd5b813581811115611ce757600080fd5b886020828501011115611cf957600080fd5b95989497505060200194505050565b600181811c90821680611d1c57607f821691505b602082108103611d3c57634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052601160045260246000fd5b600060018201611d6a57611d6a611d42565b5060010190565b818103818111156104b7576104b7611d42565b808201808211156104b7576104b7611d42565b634e487b7160e01b600052601260045260246000fd5b600082611dbc57611dbc611d97565b500690565b6001600160a01b038316815260406020820181905260009061137790830184611974565b634e487b7160e01b600052603260045260246000fd5b60008085851115611e0b57600080fd5b83861115611e1857600080fd5b5050820193919092039150565b803560208310156104b757600019602084900360031b1b1692915050565b6020808252602b908201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960408201526a6e697469616c697a696e6760a81b606082015260800190565b600081611e9d57611e9d611d42565b506000190190565b634e487b7160e01b600052600160045260246000fd5b601f8211156111dc57600081815260208120601f850160051c81016020861015611ee25750805b601f850160051c820191505b81811015610b1757828155600101611eee565b815167ffffffffffffffff811115611f1b57611f1b611aa0565b611f2f81611f298454611d08565b84611ebb565b602080601f831160018114611f645760008415611f4c5750858301515b600019600386901b1c1916600185901b178555610b17565b600085815260208120601f198616915b82811015611f9357888601518255948401946001909101908401611f74565b5085821015611fb15787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b600082611fd057611fd0611d97565b50049056feddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa2646970667358221220ceae916e2fad24f9aaa4340b6994418d32d33c6ae1f266c50dd4ec5ccbdbce2764736f6c63430008130033"
             } ==
               List.first(coin_balances)
    end

    test "parses optimism coin balance and contract code" do
      coin_balances = Importer.genesis_accounts(@optimism_genesis)

      assert Enum.count(coin_balances) == 2

      assert %{
               address_hash: %Explorer.Chain.Hash{
                 byte_count: 20,
                 bytes: <<116, 214, 181, 2, 131, 172, 29, 101, 31, 154, 253, 195, 53, 33, 228, 193, 227, 51, 43, 120>>
               },
               value: 0,
               contract_code:
                 "0x608060405234801561001057600080fd5b506004361061011d5760003560e01c8063245a7bfc14610122578063313ce5671461014657806350d25bcd1461016457806354fd4d501461017e57806358303b10146101865780636001ac53146101a5578063668a0f021461020f5780637284e4161461021757806379ba5097146102945780638205bf6a1461029e5780638da5cb5b146102a65780638f6b4d91146102ae57806392eefe9b146102b65780639a6fc8f5146102dc578063a928c09614610302578063b5ab58dc14610328578063b633620c14610345578063bc43cbaf14610362578063c15973041461036a578063e8c4be301461038b578063f2fde38b14610393578063f8a2abd3146103b9578063feaf968c146103df575b600080fd5b61012a6103e7565b604080516001600160a01b039092168252519081900360200190f35b61014e6103fc565b6040805160ff9092168252519081900360200190f35b61016c610480565b60408051918252519081900360200190f35b61016c610588565b61018e6105db565b6040805161ffff9092168252519081900360200190f35b6101cb600480360360208110156101bb57600080fd5b50356001600160501b03166105e5565b60405180866001600160501b03168152602001858152602001848152602001838152602001826001600160501b031681526020019550505050505060405180910390f35b61016c61074e565b61021f610850565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610259578181015183820152602001610241565b50505050905090810190601f1680156102865780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61029c610993565b005b61016c610a51565b61012a610b53565b6101cb610b62565b61029c600480360360208110156102cc57600080fd5b50356001600160a01b0316610cc9565b6101cb600480360360208110156102f257600080fd5b50356001600160501b0316610cf4565b61029c6004803603602081101561031857600080fd5b50356001600160a01b0316610dff565b61016c6004803603602081101561033e57600080fd5b5035610ed7565b61016c6004803603602081101561035b57600080fd5b5035610fe1565b61012a6110e4565b61012a6004803603602081101561038057600080fd5b503561ffff166110f3565b61012a611116565b61029c600480360360208110156103a957600080fd5b50356001600160a01b0316611125565b61029c600480360360208110156103cf57600080fd5b50356001600160a01b0316611181565b6101cb6111e3565b6004546201000090046001600160a01b031690565b6000600460000160029054906101000a90046001600160a01b03166001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b15801561044f57600080fd5b505afa158015610463573d6000803e3d6000fd5b505050506040513d602081101561047957600080fd5b5051905090565b6005546000906001600160a01b031680158061053d575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b15801561051057600080fd5b505afa158015610524573d6000803e3d6000fd5b505050506040513d602081101561053a57600080fd5b50515b61057a576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b6105826112ed565b91505090565b6000600460000160029054906101000a90046001600160a01b03166001600160a01b03166354fd4d506040518163ffffffff1660e01b815260040160206040518083038186803b15801561044f57600080fd5b60045461ffff1690565b60055460009081908190819081906001600160a01b03168015806106aa575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b15801561067d57600080fd5b505afa158015610691573d6000803e3d6000fd5b505050506040513d60208110156106a757600080fd5b50515b6106e7576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b6002546001600160a01b0316610732576040805162461bcd60e51b815260206004820152601e6024820152600080516020611b2c833981519152604482015290519081900360640190fd5b61073b87611340565b939b929a50909850965090945092505050565b6005546000906001600160a01b031680158061080b575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b1580156107de57600080fd5b505afa1580156107f2573d6000803e3d6000fd5b505050506040513d602081101561080857600080fd5b50515b610848576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b61058261143d565b6060600460000160029054906101000a90046001600160a01b03166001600160a01b0316637284e4166040518163ffffffff1660e01b815260040160006040518083038186803b1580156108a357600080fd5b505afa1580156108b7573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405260208110156108e057600080fd5b8101908080516040519392919084600160201b8211156108ff57600080fd5b90830190602082018581111561091457600080fd5b8251600160201b81118282018810171561092d57600080fd5b82525081516020918201929091019080838360005b8381101561095a578181015183820152602001610942565b50505050905090810190601f1680156109875780820380516001836020036101000a031916815260200191505b50604052505050905090565b61099b6114ec565b6001600160a01b0316336001600160a01b0316146109f9576040805162461bcd60e51b815260206004820152601660248201527526bab9ba10313290383937b837b9b2b21037bbb732b960511b604482015290519081900360640190fd5b6000610a036114fb565b9050610a0e3361150a565b610a18600061152c565b60405133906001600160a01b038316907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a350565b6005546000906001600160a01b0316801580610b0e575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b158015610ae157600080fd5b505afa158015610af5573d6000803e3d6000fd5b505050506040513d6020811015610b0b57600080fd5b50515b610b4b576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b61058261154e565b6000610b5d6114fb565b905090565b60055460009081908190819081906001600160a01b0316801580610c27575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b158015610bfa57600080fd5b505afa158015610c0e573d6000803e3d6000fd5b505050506040513d6020811015610c2457600080fd5b50515b610c64576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b6002546001600160a01b0316610caf576040805162461bcd60e51b815260206004820152601e6024820152600080516020611b2c833981519152604482015290519081900360640190fd5b610cb76115a1565b95509550955095509550509091929394565b610cd233611697565b600580546001600160a01b0319166001600160a01b0392909216919091179055565b60055460009081908190819081906001600160a01b0316801580610db9575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b158015610d8c57600080fd5b505afa158015610da0573d6000803e3d6000fd5b505050506040513d6020811015610db657600080fd5b50515b610df6576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b61073b87611700565b610e0833611697565b6002546001600160a01b03828116911614610e68576040805162461bcd60e51b815260206004820152601b60248201527a24b73b30b634b210383937b837b9b2b21030b3b3b932b3b0ba37b960291b604482015290519081900360640190fd5b600454600280546001600160a01b03191690556201000090046001600160a01b0316610e93826117f9565b816001600160a01b0316816001600160a01b03167f33745f67a407dcb785417f9c123dd3641479a102674b6e35c1f10975625b90e960405160405180910390a35050565b6005546000906001600160a01b0316801580610f94575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b158015610f6757600080fd5b505afa158015610f7b573d6000803e3d6000fd5b505050506040513d6020811015610f9157600080fd5b50515b610fd1576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b610fda83611868565b9392505050565b6005546000906001600160a01b031680158061109e575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b15801561107157600080fd5b505afa158015611085573d6000803e3d6000fd5b505050506040513d602081101561109b57600080fd5b50515b6110db576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b610fda83611942565b6005546001600160a01b031681565b61ffff81166000908152600360205260409020546001600160a01b03165b919050565b6002546001600160a01b031690565b61112e33611697565b6111378161152c565b806001600160a01b03166111496114fb565b6001600160a01b03167fed8889f560326eb138920d842192f0eb3dd22b4f139c87a2c57538e05bae127860405160405180910390a350565b61118a33611697565b600280546001600160a01b0319166001600160a01b0383811691821790925560045460405191926201000090910416907fc0f151710f03d713b71d9970cee0d5b11ddc9a7552abaa3f6ee818010f21600d90600090a350565b60055460009081908190819081906001600160a01b03168015806112a8575060408051630d629b5f60e31b815233600482018181526024830193845236604484018190526001600160a01b03861694636b14daf8946000939190606401848480828437600083820152604051601f909101601f1916909201965060209550909350505081840390508186803b15801561127b57600080fd5b505afa15801561128f573d6000803e3d6000fd5b505050506040513d60208110156112a557600080fd5b50515b6112e5576040805162461bcd60e51b81526020600482015260096024820152684e6f2061636365737360b81b604482015290519081900360640190fd5b610cb76119e7565b6000600460000160029054906101000a90046001600160a01b03166001600160a01b03166350d25bcd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561044f57600080fd5b60025460009081908190819081906001600160a01b0316611396576040805162461bcd60e51b815260206004820152601e6024820152600080516020611b2c833981519152604482015290519081900360640190fd5b60025460408051639a6fc8f560e01b81526001600160501b038916600482015290516001600160a01b0390921691639a6fc8f59160248082019260a092909190829003018186803b1580156113ea57600080fd5b505afa1580156113fe573d6000803e3d6000fd5b505050506040513d60a081101561141457600080fd5b508051602082015160408301516060840151608090940151929a91995097509195509350915050565b6000611447611b14565b506040805180820182526004805461ffff8116808452620100009091046001600160a01b031660208085018290528551633345078160e11b8152955194956114dd959394929363668a0f02938281019392829003018186803b1580156114ac57600080fd5b505afa1580156114c0573d6000803e3d6000fd5b505050506040513d60208110156114d657600080fd5b5051611abc565b6001600160501b031691505090565b6001546001600160a01b031690565b6000546001600160a01b031690565b600080546001600160a01b0319166001600160a01b0392909216919091179055565b600180546001600160a01b0319166001600160a01b0392909216919091179055565b6000600460000160029054906101000a90046001600160a01b03166001600160a01b0316638205bf6a6040518163ffffffff1660e01b815260040160206040518083038186803b15801561044f57600080fd5b60025460009081908190819081906001600160a01b03166115f7576040805162461bcd60e51b815260206004820152601e6024820152600080516020611b2c833981519152604482015290519081900360640190fd5b600260009054906101000a90046001600160a01b03166001600160a01b031663feaf968c6040518163ffffffff1660e01b815260040160a06040518083038186803b15801561164557600080fd5b505afa158015611659573d6000803e3d6000fd5b505050506040513d60a081101561166f57600080fd5b5080516020820151604083015160608401516080909401519299919850965091945092509050565b61169f6114fb565b6001600160a01b0316816001600160a01b0316146116fd576040805162461bcd60e51b815260206004820152601660248201527527b7363c9031b0b63630b1363290313c9037bbb732b960511b604482015290519081900360640190fd5b50565b600080600080600080600061171d886001600160501b0316611ad6565b61ffff821660009081526003602052604090819020548151639a6fc8f560e01b81526001600160401b038416600482015291519395509193506001600160a01b0390911691639a6fc8f59160248082019260a092909190829003018186803b15801561178857600080fd5b505afa15801561179c573d6000803e3d6000fd5b505050506040513d60a08110156117b257600080fd5b508051602082015160408301516060840151608090940151929a50909850965090945092506117e5878787878787611ade565b939c929b5090995097509095509350505050565b60048054604080518082018252600161ffff80851691909101168082526001600160a01b0395909516602091820181905261ffff19909316851762010000600160b01b0319166201000084021790935560009384526003909252912080546001600160a01b0319169091179055565b60006001600160501b0382111561188157506000611111565b60008061188d84611ad6565b61ffff821660009081526003602052604090205491935091506001600160a01b0316806118c05760009350505050611111565b806001600160a01b031663b5ab58dc836040518263ffffffff1660e01b815260040180826001600160401b0316815260200191505060206040518083038186803b15801561190d57600080fd5b505afa158015611921573d6000803e3d6000fd5b505050506040513d602081101561193757600080fd5b505195945050505050565b60006001600160501b0382111561195b57506000611111565b60008061196784611ad6565b61ffff821660009081526003602052604090205491935091506001600160a01b03168061199a5760009350505050611111565b806001600160a01b031663b633620c836040518263ffffffff1660e01b815260040180826001600160401b0316815260200191505060206040518083038186803b15801561190d57600080fd5b60008060008060006119f7611b14565b506040805180820182526004805461ffff811683526201000090046001600160a01b0316602083018190528351633fabe5a360e21b815293519293909263feaf968c928281019260a0929190829003018186803b158015611a5757600080fd5b505afa158015611a6b573d6000803e3d6000fd5b505050506040513d60a0811015611a8157600080fd5b5080516020820151604083015160608401516080909401518551939a509198509650919450909250610cb79087908790879087908790611ade565b6001600160401b031660409190911b61ffff60401b161790565b604081901c91565b6000806000806000611af0868c611abc565b8a8a8a611afd8a8c611abc565b939f929e50909c509a509098509650505050505050565b60408051808201909152600080825260208201529056fe4e6f2070726f706f7365642061676772656761746f722070726573656e740000a2646970667358221220e01c79ee01ff8ee5600a342cb2ed15231329d7f3f090fbe7605e1dc18906a86964736f6c634300060c0033"
             } ==
               List.first(coin_balances)
    end

    test "parses zkatana coin balance and contract code" do
      coin_balances = Importer.genesis_accounts(@zkatana_genesis)

      assert Enum.count(coin_balances) == 9

      assert %{
               address_hash: %Explorer.Chain.Hash{
                 byte_count: 20,
                 bytes: <<208, 60, 26, 156, 47, 226, 198, 243, 146, 124, 57, 138, 144, 39, 47, 233, 91, 211, 205, 175>>
               },
               value: 0,
               contract_code:
                 "0x60806040526004361061006e575f3560e01c8063715018a61161004c578063715018a6146100e25780638da5cb5b146100f6578063e11ae6cb1461011f578063f2fde38b14610132575f80fd5b80632b79805a146100725780634a94d487146100875780636d07dbf81461009a575b5f80fd5b610085610080366004610908565b610151565b005b6100856100953660046109a2565b6101c2565b3480156100a5575f80fd5b506100b96100b43660046109f5565b610203565b60405173ffffffffffffffffffffffffffffffffffffffff909116815260200160405180910390f35b3480156100ed575f80fd5b50610085610215565b348015610101575f80fd5b505f5473ffffffffffffffffffffffffffffffffffffffff166100b9565b61008561012d366004610a15565b610228565b34801561013d575f80fd5b5061008561014c366004610a61565b61028e565b61015961034a565b5f6101658585856103ca565b90506101718183610527565b5060405173ffffffffffffffffffffffffffffffffffffffff821681527fba82f25fed02cd2a23d9f5d11c2ef588d22af5437cbf23bfe61d87257c480e4c9060200160405180910390a15050505050565b6101ca61034a565b6101d583838361056a565b506040517f25adb19089b6a549831a273acdf7908cff8b7ee5f551f8d1d37996cf01c5df5b905f90a1505050565b5f61020e8383610598565b9392505050565b61021d61034a565b6102265f6105a4565b565b61023061034a565b5f61023c8484846103ca565b60405173ffffffffffffffffffffffffffffffffffffffff821681529091507fba82f25fed02cd2a23d9f5d11c2ef588d22af5437cbf23bfe61d87257c480e4c9060200160405180910390a150505050565b61029661034a565b73ffffffffffffffffffffffffffffffffffffffff811661033e576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201527f646472657373000000000000000000000000000000000000000000000000000060648201526084015b60405180910390fd5b610347816105a4565b50565b5f5473ffffffffffffffffffffffffffffffffffffffff163314610226576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152606401610335565b5f83471015610435576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f437265617465323a20696e73756666696369656e742062616c616e63650000006044820152606401610335565b81515f0361049f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820181905260248201527f437265617465323a2062797465636f6465206c656e677468206973207a65726f6044820152606401610335565b8282516020840186f5905073ffffffffffffffffffffffffffffffffffffffff811661020e576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f437265617465323a204661696c6564206f6e206465706c6f79000000000000006044820152606401610335565b606061020e83835f6040518060400160405280601e81526020017f416464726573733a206c6f772d6c6576656c2063616c6c206661696c65640000815250610618565b6060610590848484604051806060016040528060298152602001610b0860299139610618565b949350505050565b5f61020e83833061072d565b5f805473ffffffffffffffffffffffffffffffffffffffff8381167fffffffffffffffffffffffff0000000000000000000000000000000000000000831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b6060824710156106aa576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f60448201527f722063616c6c00000000000000000000000000000000000000000000000000006064820152608401610335565b5f808673ffffffffffffffffffffffffffffffffffffffff1685876040516106d29190610a9c565b5f6040518083038185875af1925050503d805f811461070c576040519150601f19603f3d011682016040523d82523d5f602084013e610711565b606091505b509150915061072287838387610756565b979650505050505050565b5f604051836040820152846020820152828152600b8101905060ff815360559020949350505050565b606083156107eb5782515f036107e45773ffffffffffffffffffffffffffffffffffffffff85163b6107e4576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610335565b5081610590565b61059083838151156108005781518083602001fd5b806040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016103359190610ab7565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b5f82601f830112610870575f80fd5b813567ffffffffffffffff8082111561088b5761088b610834565b604051601f83017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0908116603f011681019082821181831017156108d1576108d1610834565b816040528381528660208588010111156108e9575f80fd5b836020870160208301375f602085830101528094505050505092915050565b5f805f806080858703121561091b575f80fd5b8435935060208501359250604085013567ffffffffffffffff80821115610940575f80fd5b61094c88838901610861565b93506060870135915080821115610961575f80fd5b5061096e87828801610861565b91505092959194509250565b803573ffffffffffffffffffffffffffffffffffffffff8116811461099d575f80fd5b919050565b5f805f606084860312156109b4575f80fd5b6109bd8461097a565b9250602084013567ffffffffffffffff8111156109d8575f80fd5b6109e486828701610861565b925050604084013590509250925092565b5f8060408385031215610a06575f80fd5b50508035926020909101359150565b5f805f60608486031215610a27575f80fd5b8335925060208401359150604084013567ffffffffffffffff811115610a4b575f80fd5b610a5786828701610861565b9150509250925092565b5f60208284031215610a71575f80fd5b61020e8261097a565b5f5b83811015610a94578181015183820152602001610a7c565b50505f910152565b5f8251610aad818460208701610a7a565b9190910192915050565b602081525f8251806020840152610ad5816040850160208701610a7a565b601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe016919091016040019291505056fe416464726573733a206c6f772d6c6576656c2063616c6c20776974682076616c7565206661696c6564a2646970667358221220330b94dc698c4d290bf55c23f13b473cde6a6bae0030cb902de18af54e35839f64736f6c63430008140033"
             } ==
               List.first(coin_balances)
    end
  end

  describe "import_genesis_accounts/1" do
    test "imports accounts" do
      block_quantity = integer_to_quantity(1)
      res = eth_block_number_fake_response(block_quantity)

      EthereumJSONRPC.Mox
      |> expect(:json_rpc, fn [
                                %{id: 0, jsonrpc: "2.0", method: "eth_getBlockByNumber", params: ["0x1", true]}
                              ],
                              _opts ->
        {:ok, [res]}
      end)

      {:ok, %{address_coin_balances: address_coin_balances}} = Importer.import_genesis_accounts(@geth_genesis)

      assert Enum.count(address_coin_balances) == 3
      assert CoinBalance |> Repo.all() |> Enum.count() == 3
      assert CoinBalanceDaily |> Repo.all() |> Enum.count() == 3
      assert Address |> Repo.all() |> Enum.count() == 3
    end

    test "imports contract code" do
      block_quantity = integer_to_quantity(1)
      res = eth_block_number_fake_response(block_quantity)

      EthereumJSONRPC.Mox
      |> expect(:json_rpc, fn [
                                %{id: 0, jsonrpc: "2.0", method: "eth_getBlockByNumber", params: ["0x1", true]}
                              ],
                              [] ->
        {:ok, [res]}
      end)

      code =
        "0x608060405234801561001057600080fd5b50600436106100cf5760003560e01c806391ad27b41161008c57806398d5fdca1161006657806398d5fdca14610262578063a97e5c9314610280578063df5dd1a5146102dc578063eebd48b014610320576100cf565b806391ad27b4146101e457806391b7f5ed14610202578063955d14cd14610244576100cf565b80630aa6f2fe146100d457806320ba81ee1461011657806322a90082146101345780634c2c987c14610176578063764cbcd1146101985780637837efdc146101da575b600080fd5b610100600480360360208110156100ea57600080fd5b8101908080359060200190929190505050610353565b6040518082815260200191505060405180910390f35b61011e6103c4565b6040518082815260200191505060405180910390f35b6101606004803603602081101561014a57600080fd5b81019080803590602001909291905050506103ce565b6040518082815260200191505060405180910390f35b61017e61043f565b604051808215151515815260200191505060405180910390f35b6101c4600480360360208110156101ae57600080fd5b8101908080359060200190929190505050610456565b6040518082815260200191505060405180910390f35b6101e26104c7565b005b6101ec6104d2565b6040518082815260200191505060405180910390f35b61022e6004803603602081101561021857600080fd5b81019080803590602001909291905050506104dc565b6040518082815260200191505060405180910390f35b61024c6106a2565b6040518082815260200191505060405180910390f35b61026a6106ac565b6040518082815260200191505060405180910390f35b6102c26004803603602081101561029657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506106b6565b604051808215151515815260200191505060405180910390f35b61031e600480360360208110156102f257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506106d3565b005b61032861073d565b6040518085815260200184815260200183815260200182815260200194505050505060405180910390f35b600061035e336106b6565b6103b3576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526030815260200180610b4f6030913960400191505060405180910390fd5b816004819055506004549050919050565b6000600454905090565b60006103d9336106b6565b61042e576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526030815260200180610b4f6030913960400191505060405180910390fd5b816003819055506003549050919050565b6000600560009054906101000a900460ff16905090565b6000610461336106b6565b6104b6576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526030815260200180610b4f6030913960400191505060405180910390fd5b816002819055506002549050919050565b6104d033610771565b565b6000600354905090565b60006104e7336106b6565b61053c576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526030815260200180610b4f6030913960400191505060405180910390fd5b600082116105b2576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260098152602001807f7072696365203c3d30000000000000000000000000000000000000000000000081525060200191505060405180910390fd5b6105ba6104d2565b6105c26106a2565b01421015610638576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f54494d455f4c4f434b5f494e434f4d504c45544500000000000000000000000081525060200191505060405180910390fd5b610641826107cb565b5061064b42610456565b503373ffffffffffffffffffffffffffffffffffffffff167f95dce27040c59c8b1c445b284f81a3aaae6eecd7d08d5c7684faee64cdb514a1836040518082815260200191505060405180910390a2819050919050565b6000600254905090565b6000600154905090565b60006106cc82600061083c90919063ffffffff16565b9050919050565b6106dc336106b6565b610731576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526030815260200180610b4f6030913960400191505060405180910390fd5b61073a8161091a565b50565b60008060008061074b6106ac565b6107536104d2565b61075b6103c4565b6107636106a2565b935093509350935090919293565b61078581600061097390919063ffffffff16565b8073ffffffffffffffffffffffffffffffffffffffff167f9c8e7d83025bef8a04c664b2f753f64b8814bdb7e27291d7e50935f18cc3c71260405160405180910390a250565b60006107d6336106b6565b61082b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526030815260200180610b4f6030913960400191505060405180910390fd5b816001819055506001549050919050565b60008073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156108c3576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526022815260200180610b2d6022913960400191505060405180910390fd5b8260000160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16905092915050565b61092e816000610a3090919063ffffffff16565b8073ffffffffffffffffffffffffffffffffffffffff167e47706786c922d17b39285dc59d696bafea72c0b003d3841ae1202076f4c2e460405160405180910390a250565b61097d828261083c565b6109d2576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526021815260200180610b0c6021913960400191505060405180910390fd5b60008260000160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff0219169083151502179055505050565b610a3a828261083c565b15610aad576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601f8152602001807f526f6c65733a206163636f756e7420616c72656164792068617320726f6c650081525060200191505060405180910390fd5b60018260000160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff021916908315150217905550505056fe526f6c65733a206163636f756e7420646f6573206e6f74206861766520726f6c65526f6c65733a206163636f756e7420697320746865207a65726f20616464726573734f7261636c65526f6c653a2063616c6c657220646f6573206e6f74206861766520746865204f7261636c6520726f6c65a265627a7a72315820df30730da57a5061c487e0b37e84e80308fa443e2e80ee9117a13fa8149caf4164736f6c634300050b0032"

      chain_spec = %{
        "alloc" => %{
          "0xcd59f3dde77e09940befb6ee58031965cae7a336" => %{
            "balance" => "0x21e19e0c9bab2400000",
            "code" => code
          }
        }
      }

      {:ok, _} = Importer.import_genesis_accounts(chain_spec)

      address = Address |> Repo.one()

      assert to_string(address.contract_code) == code
    end

    test "imports polygon accounts" do
      block_quantity = integer_to_quantity(1)
      res = eth_block_number_fake_response(block_quantity)

      EthereumJSONRPC.Mox
      |> expect(:json_rpc, fn [
                                %{id: 0, jsonrpc: "2.0", method: "eth_getBlockByNumber", params: ["0x1", true]}
                              ],
                              _ ->
        {:ok, [res]}
      end)

      {:ok, %{address_coin_balances: address_coin_balances}} = Importer.import_genesis_accounts(@polygon_genesis)

      assert Enum.count(address_coin_balances) == 2
      assert CoinBalance |> Repo.all() |> Enum.count() == 2
      assert CoinBalanceDaily |> Repo.all() |> Enum.count() == 2
      assert Address |> Repo.all() |> Enum.count() == 2
    end

    test "imports optimism accounts" do
      block_quantity = integer_to_quantity(1)
      res = eth_block_number_fake_response(block_quantity)

      EthereumJSONRPC.Mox
      |> expect(:json_rpc, fn [
                                %{id: 0, jsonrpc: "2.0", method: "eth_getBlockByNumber", params: ["0x1", true]}
                              ],
                              _ ->
        {:ok, [res]}
      end)

      {:ok, %{address_coin_balances: address_coin_balances}} = Importer.import_genesis_accounts(@optimism_genesis)

      assert Enum.count(address_coin_balances) == 2
      assert CoinBalance |> Repo.all() |> Enum.count() == 2
      assert CoinBalanceDaily |> Repo.all() |> Enum.count() == 2
      assert Address |> Repo.all() |> Enum.count() == 2
    end
  end

  defp eth_block_number_fake_response(block_quantity) do
    %{
      id: 0,
      jsonrpc: "2.0",
      result: %{
        "author" => "0x0000000000000000000000000000000000000000",
        "difficulty" => "0x20000",
        "extraData" => "0x",
        "gasLimit" => "0x663be0",
        "gasUsed" => "0x0",
        "hash" => "0x5b28c1bfd3a15230c9a46b399cd0f9a6920d432e85381cc6a140b06e8410112f",
        "logsBloom" =>
          "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        "miner" => "0x0000000000000000000000000000000000000000",
        "number" => block_quantity,
        "parentHash" => "0x0000000000000000000000000000000000000000000000000000000000000000",
        "receiptsRoot" => "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
        "sealFields" => [
          "0x80",
          "0xb8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
        ],
        "sha3Uncles" => "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
        "signature" =>
          "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        "size" => "0x215",
        "stateRoot" => "0xfad4af258fd11939fae0c6c6eec9d340b1caac0b0196fd9a1bc3f489c5bf00b3",
        "step" => "0",
        "timestamp" => "0x0",
        "totalDifficulty" => "0x20000",
        "transactions" => [],
        "transactionsRoot" => "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
        "uncles" => []
      }
    }
  end
end
